package com.zsl.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.zsl.dao.BookDao;
import com.zsl.dao.OrderInfoDao;
import com.zsl.dao.OrderItemDao;
import com.zsl.dao.UsersDao;
import com.zsl.dao.impl.BookDaoImpl;
import com.zsl.dao.impl.OrderInfoDaoImpl;
import com.zsl.dao.impl.OrderItemDaoImpl;
import com.zsl.dao.impl.UsersDaoImpl;
import com.zsl.model.Book;
import com.zsl.model.OrderInfo;
import com.zsl.model.OrderItem;
import com.zsl.service.OrderItemService;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OrderItemServiceImpl implements OrderItemService {
    UsersDao usersDao = new UsersDaoImpl();
    OrderItemDao orderItemDao = new OrderItemDaoImpl();
    OrderInfoDao orderInfoDao = new OrderInfoDaoImpl();
    BookDao bookDao = new BookDaoImpl();

    @Override
    public Map<String, Object> getShoppingCartOrderItem(String u_id) throws SQLException {
        Map<String, Object> result = new HashMap<>();
        JSONArray rows = new JSONArray();
        int total = 0;
        //用户号有问题
        if (u_id == null || u_id.equals("")) {
            result.put("total", "0");
        }
        // 获取这个用户的购物车
        else {
            //这个用户是否有购物车
            if (orderInfoDao.getOrderInfo(new String[]{"user_id", "status"}, new String[]{u_id, "0"})
                    .isEmpty()) {
                result.put("total", "0");
                result.put("rows", rows);
                return result;
            }
            //获取用户的购物车信息
            OrderInfo user_orderinfo = orderInfoDao.getOrderInfo(
                    new String[]{"user_id", "status"},
                    new String[]{u_id, "0"}
            ).get(0);
            //获取用户的购物车条目列表
            List<OrderItem> user_orderitems = orderItemDao.getOrderItem(
                    new String[]{"orderInfo_id"},
                    new String[]{user_orderinfo.getId()}
            );
            //将列表生成json并返回
            for (OrderItem orderItem : user_orderitems) {
                Map<String, Object> orderitem_map = new HashMap<>();
                orderitem_map.put("item_id", orderItem.getId());
                orderitem_map.put("b_num", orderItem.getNum());
                //获取书本的信息
                Book book = bookDao.getBookById(orderItem.getBook_id());
                orderitem_map.put("b_price", book.getPrice());
                orderitem_map.put("i_price", orderItem.getPrice());
                orderitem_map.put("b_id", orderItem.getBook_id());
                orderitem_map.put("b_name", orderItem.getBook_generate_name());
                rows.add(orderitem_map);
                total++;
            }
            result.put("total", total);
        }
        result.put("rows", rows);
        return result;
    }

    @Override
    public Map<String, Object> deleteShoppingCartItem(String u_id, String item_id) throws SQLException {
        Map<String, Object> result = new HashMap<>();
        if (u_id == null || u_id.equals("")) {
            result.put("code", "0");
            result.put("msg", "用户出错");
            return result;
        }
        // 获取该用户的购物车
        List<OrderInfo> user_orderInfos = orderInfoDao.getOrderInfo(
                new String[]{"user_id", "status"},
                new String[]{u_id, "0"}
        );
        if (user_orderInfos.isEmpty()) {
            result.put("code", "0");
            result.put("msg", "用户没有购物车");
            return result;
        }
        final OrderInfo user_orderinfo = user_orderInfos.get(0);
        // 获取待删除条目信息
        List<OrderItem> orderItems = orderItemDao.getOrderItem(
                new String[]{"orderitem.id"},
                new String[]{item_id}
        );
        if (orderItems.isEmpty()) {
            result.put("code", "0");
            result.put("msg", "用户购物车没有该物品，无法删除");
            return result;
        }
        OrderItem orderItem = orderItems.get(0);
        // 删除操作
        if (orderItemDao.deleteOrderItemById(orderItem.getId()) != 1) {
            result.put("code", "0");
            result.put("msg", "删除失败");
            return result;
        }
        // 更新删除后的订单价格
        user_orderinfo.setNum(user_orderinfo.getNum() - 1);
        //减去原先价格
        user_orderinfo.setPrice(user_orderinfo.getPrice() - orderItem.getPrice());
        orderInfoDao.updateOrderInfoById(new ArrayList<OrderInfo>() {{
            add(user_orderinfo);
        }}, 1);
        result.put("code","1");
        result.put("msg","删除成功");
        return result;
    }

    @Override
    public Map<String, Object> getOrderItemByOrderId(String OrderId) throws SQLException {
        Map<String, Object> result = new HashMap<>();
        JSONArray rows = new JSONArray();
        int total = 0;

        // 先通过orderId获得orderinfo_id
        List<OrderInfo> orderInfos=orderInfoDao.getOrderInfo(
                new String[]{"orderId"},
                new String[]{OrderId}
        );
        if(orderInfos.isEmpty()){
            result.put("total","0");
            result.put("rows",rows);
            return result;
        }
        //获取orderitem
        List<OrderItem> orderItems=orderItemDao.getOrderItem(
                new String[]{"orderInfo_id"},
                new String[]{orderInfos.get(0).getId()}
        );
        if(orderItems.isEmpty()){
            result.put("total","0");
            result.put("rows",rows);
            return result;
        }
        for (OrderItem orderItem : orderItems) {
            Map<String, Object> orderitem_map = new HashMap<>();
            orderitem_map.put("item_id", orderItem.getId());
            orderitem_map.put("b_num", orderItem.getNum());
            //获取书本的信息
            Book book = bookDao.getBookById(orderItem.getBook_id());
            orderitem_map.put("b_price", book.getPrice());
            orderitem_map.put("i_price", orderItem.getPrice());
            orderitem_map.put("b_id", orderItem.getBook_id());
            orderitem_map.put("b_name", orderItem.getBook_generate_name());
            rows.add(orderitem_map);
            total++;
        }
        result.put("total", total);
        result.put("rows", rows);
        return result;
    }
}
